﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class ProductDAO
    {
        static DataClasses1DataContext Data = new DataClasses1DataContext();
        public static List<PRODUCT> GetAllProduct()
        {
            try
            {
                var query = from product in Data.PRODUCTs where (product.VISIBLE==true) select product;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pr in query)
                {
                    ListProduct.Add(pr);
                }
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static PRODUCT GetProduct(int IDProduct)
        {
            try
            {
                PRODUCT Product = Data.PRODUCTs.Single(pr => pr.IDPRODUCT == IDProduct);
                return Product;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PRODUCT> GetProductToShow()
        {
            try
            {
                var query = from pro in Data.PRODUCTs where ((pro.STATUS.Value == 1) &&((pro.TIME_START.Value -DateTime.Now).TotalSeconds<0)
                            &&((pro.TIME_STOP.Value-DateTime.Now).TotalSeconds>0)&& (pro.VISIBLE==true)) select pro;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    if(pro.OrderedNumber<pro.MAX_NUMBER)
                    ListProduct.Add(pro);
                }

                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// lấy danh sách sản phẩm theo thành phố de show ra man hinh
        /// </summary>
        /// <param name="City"></param>
        /// <returns></returns>
        public static List<PRODUCT> GetProductToShowByCity(string City)
        {
            try
            {
                var query = from pro in Data.PRODUCTs where( (pro.STATUS.Value == 1) &&((pro.TIME_START.Value -DateTime.Now).TotalSeconds<0)
                            &&((pro.TIME_STOP.Value-DateTime.Now).TotalSeconds>0) && (pro.CITY.Trim() == City.Trim())&& (pro.VISIBLE==true) )select pro;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    if (pro.OrderedNumber < pro.MAX_NUMBER)
                    ListProduct.Add(pro);
                }
                
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy danh sách sản phẩm đã đăng .được sắp xếp theo thứ tự thằng nào mới posted thì đứng trước
        /// </summary>
        /// <returns></returns>
        public static List<PRODUCT> GetProductEnded()
        {
            try
            {
                var query = from pro in Data.PRODUCTs where ((((pro.TIME_STOP.Value - DateTime.Now).TotalSeconds<0)|| (pro.OrderedNumber>=pro.MAX_NUMBER))&& (pro.STATUS.Value==1)&&(pro.VISIBLE==true)) select pro;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    ListProduct.Add(pro);
                }

                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PRODUCT> GetProductEndedByCity(string City)
        {
            try
            {
                var query = from product in Data.PRODUCTs where ((((product.TIME_STOP.Value - DateTime.Now).TotalSeconds<0)||(product.OrderedNumber>=product.MAX_NUMBER)) && (product.CITY.Trim() == City.Trim())
                                && (product.STATUS.Value == 1)&&(product.VISIBLE==true)) select product;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    ListProduct.Add(pro);
                }

                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy danh sách sản phẩm sắp khuyến mãi.sắp xếp theo thứ tự thằng nào gần khuyến mãi nhất thì đứng trước
        /// </summary>
        /// <returns></returns>
        public static List<PRODUCT> GetProductSapKhuyenMai()
        {
            try
            {
                var query = from pro in Data.PRODUCTs where (((pro.TIME_START.Value - DateTime.Now).TotalSeconds>0)&& (pro.STATUS.Value==1)&&(pro.VISIBLE==true)) select pro;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    ListProduct.Add(pro);
                }
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PRODUCT> GetProductSapKhuyenMai(string City)
        {
            try
            {
                var query = from product in Data.PRODUCTs where(((product.TIME_START.Value - DateTime.Now).TotalSeconds>0)&& (product.CITY.Trim()==City.Trim())
                                && (product.STATUS.Value==1) &&(product.VISIBLE==true))select product;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    ListProduct.Add(pro);
                }
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PRODUCT> Search(string stringSearch)
        {
            try
            {
                var query = from Product in Data.PRODUCTs where (Product.VISIBLE==true) select Product;
                List<PRODUCT> ResultSearch = new List<PRODUCT>();
                foreach(PRODUCT pro in query)
                {
                    if (pro.Name_Product.ToLower().Contains(stringSearch) || pro.SLOGAN.ToLower().Contains(stringSearch))
                        ResultSearch.Add(pro);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static bool Insert(PRODUCT Product)
        {
            try
            {
                Data.PRODUCTs.InsertOnSubmit(Product);
                Data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool Update(PRODUCT m_Product)
        {
            try
            {
                PRODUCT product = Data.PRODUCTs.Single(pr => pr.IDPRODUCT == m_Product.IDPRODUCT);
                product.CITY = m_Product.CITY;
                product.Conditions_Apply = m_Product.Conditions_Apply;
                product.CONTENT = m_Product.CONTENT;
                product.Features = m_Product.Features;
                product.MAX_NUMBER = m_Product.MAX_NUMBER;
                product.MIN_NUMBER = m_Product.MIN_NUMBER;
                product.Name_Product = m_Product.Name_Product;
                product.OrderedNumber = m_Product.OrderedNumber;
                product.PRICE = m_Product.PRICE;
                product.PurchasedNumber = m_Product.PurchasedNumber;
                product.RATE_DECREASE = m_Product.RATE_DECREASE;
                product.SLOGAN = m_Product.SLOGAN;
                product.STATUS = m_Product.STATUS;
                product.TIME_START = m_Product.TIME_START;
                product.TIME_STOP = m_Product.TIME_STOP;
                Data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static int GetMaxID()
        {
            int IDMax = Data.PRODUCTs.Max(pr => pr.IDPRODUCT);
            return IDMax;
        }
        public static bool IsExisting(int IDProduct)
        {
            try
            {
                return Data.PRODUCTs.Any(Product => ((Product.IDPRODUCT == IDProduct)&& (Product.VISIBLE==true)));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool Delete(int IDProduct)
        {
            try
            {
                PRODUCT Product = Data.PRODUCTs.Single(pr => pr.IDPRODUCT == IDProduct);
                Product.VISIBLE = false;
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static List<PRODUCT> GetProductNotCheck()
        {
            try
            {
                var query = from product in Data.PRODUCTs where ((product.STATUS.Value == 0) && (product.VISIBLE == true)) select product;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    ListProduct.Add(pro);
                }
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PRODUCT> GetProductNotCheck(string City)
        {
            try
            {
                var query = from product in Data.PRODUCTs where ((product.STATUS.Value == 0) && (string.Compare(product.CITY, City, true) == 0) && (product.VISIBLE == true)) select product;
                List<PRODUCT> ListProduct = new List<PRODUCT>();
                foreach (PRODUCT pro in query)
                {
                    ListProduct.Add(pro);
                }
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void ApproveProduct(int id)
        {
            try
            {
                PRODUCT Product = Data.PRODUCTs.Single(pro => pro.IDPRODUCT == id);
                Product.STATUS = 1;
                Data.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool AddOrderNumber(int id, int quality)
        {
            try
            {
                PRODUCT Product = Data.PRODUCTs.Single(pro => pro.IDPRODUCT == id);
                Product.OrderedNumber += quality;
                Data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
           
    }
}
